package org.libra {
	import flash.events.Event;
	import org.libra.basic.Component;
	import org.libra.basic.Container;
	import org.libra.geom.IntDimension;
	import org.libra.geom.IntPoint;
	import org.libra.ui.interfaces.IButtonState;
	import org.libra.utils.JColor;
	
	/**
	 * ...
	 * @author zhangzuoqiang
	 */
	public class JFrame extends JPanel {
		
		private var titleBar:JFrameTitleBar;
		private var titleBarHeight:int;
		private var titleDragable:Boolean;
		
		public function JFrame(owner:Container, modal:Boolean = false, titleBarHeight:int = 20, titleDragable:Boolean = true) { 
			super(owner, modal);
			super.setCloseable(false);
			this.titleDragable = titleDragable;
			this.titleBarHeight = titleBarHeight;
			titleBar = new JFrameTitleBar(this);
		}
		
		override public function append(c:Component):Boolean {
			if (c is JFrame) {
				throw new Error("A JFrame can't be the other JFrame's parent!");
				return false;
			}
			return super.append(c);
		}
		
		override public function show():void {
			super.show();
			if (showing) {
				this.titleBar.show();
				if (titleDragable) {
					this.titleBar.addDragListener();
				}
				if (titleBar.isCloseable()) {
					titleBar.addEventListener(JFrameTitleBar.TITLE_BAR_CLOSE, onTitleBarCloseHandler);
				}
			}
		}
		
		override public function tryToClose():void {
			if (titleBar.isCloseable()) {
				this.titleBar.tryToClose();
			}else {
				onTitleBarCloseHandler(null);
			}
		}
		
		override public function remove(c:Component, removeOwner:Boolean = true):Boolean {
			var result:Boolean = super.remove(c, removeOwner);
			if (c == titleBar) {
				titleBar.setShowing(false);
			}
			return result;
		}
		
		override public function removeAll():void {
			super.removeAll();
			this.titleBar.setShowing(false);
		}
		
		private function onTitleBarCloseHandler(e:Event):void {
			super.tryToClose();
			if (!showing) {
				if (titleDragable) {
					this.titleBar.removeDragListener();
				}
				if (titleBar.isCloseable()) {
					titleBar.removeEventListener(JFrameTitleBar.TITLE_BAR_CLOSE, onTitleBarCloseHandler);
				}
			}
		}
		
		override public function setOpaque(b:Boolean):void {
			super.setOpaque(b);
			this.titleBar.setOpaque(b);
		}
		
		override public function setSize(size:IntDimension):void {
			super.setSize(size);
			this.titleBar.setSize(new IntDimension(size.getWidth(), titleBarHeight));
			if (isOpaque()) {
				paint();
			}
		}
		
		override public function paint():void {
			this.titleBar.paint();
			this.graphics.clear();
			this.addBorder(JColor.RED);
			this.graphics.beginFill(JColor.MAGENTA.getRGB());
			this.graphics.drawRect(0, 0, getWidth(), getHeight());
			this.graphics.endFill();
		}
		
		override public function setCloseable(b:Boolean):void {
			if (this.titleBar.isCloseable() != b) {
				this.titleBar.setCloseable(b);
				if (b) {
					if (showing) {
						titleBar.addEventListener(JFrameTitleBar.TITLE_BAR_CLOSE, onTitleBarCloseHandler);
					}
				}else {
					if (showing) {
						titleBar.removeEventListener(JFrameTitleBar.TITLE_BAR_CLOSE, onTitleBarCloseHandler);
					}
				}
			}
		}
		
		override public function setCloseBtnState(state:IButtonState):void {
			this.titleBar.setCloseBtnState(state);
		}
		
		override public function setCloseBtnLocation(value:IntPoint):void {
			this.titleBar.setCloseBtnLocation(value);
		}
		
		override public function isCloseable():Boolean {
			return this.titleBar.isCloseable();
		}
		
		public function setTitleDragable(drag:Boolean):void {
			if (this.titleDragable != drag) {
				if (drag) {
					this.titleBar.addDragListener();
				}else {
					this.titleBar.removeDragListener();
				}
				this.titleDragable = drag;
			}
		}
		
		public function isTitleDragable():Boolean {
			return this.titleDragable;
		}
		
		override public function toString():String {
			return "JFrame";
		}
		
	}

}